import {searchPage,deleteById,addEntity} from '../../api/brand'
import pickerOptions from  '@/utils/date';
import {getBase64Str} from '../../utils/base64Utils'
import base64 from '../../utils/getbase64Str2';


let brand = {
    data() {
        return {
            //日期组件
            pickerOptions:pickerOptions,
            //table的list
            tableData: [],
            total:0,
            searchParams:{
                currentPage :1,
                pageSize:5
            },
            dialogVisible:false,
            formData:{},
            imageUrl:''
        };
    },
    methods:{
        //复选框的change事件，删除和修改拿到id
        checkBoxSelectionChange(){

        },
        currentPageChange(currentPage){
            //console.log(currentPage);
            this.searchParams.currentPage = currentPage;
            this.findPage();
        },
      /*  findPage(){
            searchPage(this.searchParams).then(response=>{
               this.total= response.total;
               this.tableData = response.data;
            })
        },  */
      //第二种写法：
       async findPage(){
           let response =  await searchPage(this.searchParams);
               this.total= response.total;

               this.tableData = response.data;
        },
        chooseTime(){
            this.searchParams.startTime=this.pickerOptions.startArrayDate[0];
            this.searchParams.endTime=this.pickerOptions.startArrayDate[1];
        },
        resetForm(){
            this.searchParams={  currentPage :1, pageSize:5};
            this.pickerOptions.startArrayDate ='';
        },
        //async表示异步调用 await：表示执行完这条语句，才执行下面语句
       async deleteById(){
           await deleteById(this.formData.id);
           this.findPage();
        },
        // getImgStr(e){
        //     console.log("imgstr===");
        //     console.log(e.file);
        //     let fileReader = new FileReader();
        //     fileReader.readAsDataURL(e.file);
        //     fileReader.onload= e=> {
        //         console.log("onload===");
        //         console.log(e);
        //         console.log("图片的base64 e.target.result====");
        //         console.log(e.target.result);
        //         this.formData.brandLogo=e.target.result;
        //         this.imageUrl = e.target.result;
        //
        //     }
        //
        // },
       // getImgStr(e){
       //   getBase64Str(e.file).then(response=>{
       //       this.formData.brandLogo = response;
       //       this.imageUrl =  response;
       //   })
       //  },

        getImgStr(e){
            base64.getbase64Str(e.file).then(response=>{
                this.formData.brandLogo=response;
                this.imageUrl = response;
            })

        },
        async addOrEdit(){
            await addEntity(this.formData);
            this.findPage();
        }
    },
    created(){
        this.findPage();
    }
};

export default brand;